<?php

/*

  Action	$7

  Trash any number of cards from your hand.
  Gain a card with cost exactly equal to the total cost in coins of the trashed cards.

 */

class Forge extends Card implements iAction, iKingdom, iProsperity {

    public function __construct() {
        parent::__construct("Forge", 7);
        $this->undoPlay = false;
        $this->undoBuy = true;
    }

    public function getDescription() {
        return array(
            "Trash any number of cards from your hand.  Gain a card with cost exactly equal to the total cost in coins of the trashed cards.");
    }
    
    public function play(Game $game, $isRealCard) {

        $cards = $game->getPlayHand();

        if (sizeof($cards) > 0) {

            $output = MySocket::GetUserReply($game, Input_ChooseMany, $cards, 0, -1, null, "Choose cards to trash");
             ScreenDisplay::addScreenMessage($game, true, "trash " . nicePrint($output), "trashes " . ScreenDisplay::$LMSG);
            $totalPrize = new Price();
            foreach ($output as $p) {
                $card = $game->moveCardFromPileToPile($p, PlayHand, Trash);
                $totalPrize = $totalPrize->add($card->getPrice($game));
            }
            //getting only the coins
            $coins = $totalPrize->getCoins();
            $new_price = new Price($coins, 0);

            $option_cards = $game->getBoardCardsByPrice($new_price, $new_price);
            if (sizeof($option_cards) > 0) {

				$option_cards = getUniqueCards($option_cards);
				if (sizeof($option_cards) > 1) {
					$output = MySocket::GetUserReply($game, Input_ChooseOne, $option_cards, 1, 1, null, "Choose card to gain");
					$card_name = $output[0];
				}
				else {
					$card_name = $option_cards[0];
				}

                $new_card = $game->moveCardFromPileToPile($card_name, BoardCards, DiscardPile);
                ScreenDisplay::addScreenMessage($game, false, " and gain " . nicePrint($new_card), " and gains " . ScreenDisplay::$LMSG);
            }
            else {
                ScreenDisplay::addScreenMessage($game, false, " and gain nothing" , " and gains nothing");
            }
        }
        $game->cardActionFinished();
        return true;
    }

}

?>